package com.example.xianglilai_end.Service;

import com.alibaba.fastjson.JSONObject;
import com.example.xianglilai_end.Bean.*;
import com.example.xianglilai_end.Mapper.StationsMapper;
import com.example.xianglilai_end.Utils.DateUtil;
import com.example.xianglilai_end.Mapper.BatteriesMapper;
import com.example.xianglilai_end.Utils.GPSTransToAMap;
import com.example.xianglilai_end.Utils.GetCityRange;
import com.example.xianglilai_end.Utils.LocationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class BatteriesService {
    @Autowired
    private final BatteriesMapper batteriesMapper;

    public BatteriesService(BatteriesMapper batteriesMapper) {
        this.batteriesMapper = batteriesMapper;
    }

    /* 获取15min内，电池的gps */
    public BatteryGpsList getBatteriesGPS_15min(int day, int start_time, int end_time){
        BatteryGpsList batteriesList = new BatteryGpsList();
        List<DeviceId> batteries_id = new ArrayList<>();
        List<BatteryGps> batteries = new ArrayList<>();

        List<DeviceId> deviceIds = batteriesMapper.getDeviceId("bms_" + day + "0000");
        List<Gps> gpsList = batteriesMapper.getGps("xl_device_gps_" + day,start_time,end_time);
        // 循环，将每个电池的gps处理成path和timeline
        int cnt = 0;
        for (DeviceId id : deviceIds){
            BatteryGps battery = new BatteryGps();
            List<double[]> batteryPath = new ArrayList<>();
            List<Integer> batteryTimeline = new ArrayList<>();
            // 根据id查找对应的gps
            if (gpsList.size() > 0){    // gpsList不为空、gps不为空
                for (Gps gps: gpsList){
                    if (gps.getDevice_id() == id.getDevice_id()){
                        if (gps.getGps().isEmpty()){
                            continue;
                        }

                        String[] str_gps = gps.getGps().split(",");
//                        System.out.println(str_gps[0]);
                        // 排除无效坐标
                        if (str_gps[0].equals("A")){
                            double[] lat_lng = GPSTransToAMap.transform(gps.getGps());
                            if (lat_lng[0] >= 103.75 && lat_lng[0] <= 104.35 && lat_lng[1] >= 30.39 && lat_lng[1] <= 30.90) {
                                battery.setDevice_id(id.getDevice_id());
                                batteryPath.add(lat_lng);
                                batteryTimeline.add(gps.getTimeline());
                            }
                        }
                    }
                }
                if (batteryPath.size() != 0 && batteryTimeline.size() != 0){    // 确保电池的路径和时间不为空
                    batteries_id.add(id);       // 添加有用的电池id
                    battery.setPath(batteryPath);
                    battery.setTimeline(batteryTimeline);
                    batteries.add(battery);
                }
            }
//            System.out.printf("完成 %d : %d \n",cnt, deviceIds.size());
            cnt += 1;
        }
        batteriesList.setBattery_id_list(batteries_id);

        System.out.println("----------------start processing-------------------");
        // 循环所有BatteryGps，将离散的timeline和gps连续化
//        for (BatteryGps b: batteries){
        for(int k = 0;k<batteries.size();k++){
            BatteryGps b = batteries.get(k);
            List<double[]> path = b.getPath();
            List<Integer> timeline = b.getTimeline();
            List<double[]> new_path = new ArrayList<>();
            List<Integer> new_timeline = new ArrayList<>();
            // 如果第一个时间不是当前开始时间，就往前补到开始时间
            if (timeline.get(0) != start_time){
                int interval = timeline.get(0) - start_time;
                List<double[]> insert_path = new ArrayList<>(); // 要插入的集合
                List<Integer> insert_timeine = new ArrayList<>(); // 要插入的集合
                for (int i = 1; i < interval; i++){
                    insert_path.add(path.get(0));
                    insert_timeine.add(start_time + i);
                }
                new_path.addAll(0,insert_path);
                new_timeline.addAll(0,insert_timeine);
            }
            new_path.add(path.get(0));
            new_timeline.add(timeline.get(0));
            for (int i = 0; i < timeline.size()-1; i++){
                // 计算时间和gps的差距
                int timeline_offset = timeline.get(i+1) - timeline.get(i) - 1;
                // 要插入的集合
                List<double[]> insert_path = new ArrayList<>();
                List<Integer> insert_timeline = new ArrayList<>();
                // 如果中间能插入的数量大于0,就在中间插入每秒的数据
                if (timeline_offset > 0){
                    double distance_x = path.get(i+1)[0] - path.get(i)[0];
                    double distance_y = path.get(i+1)[1] - path.get(i)[1];
                    double delta_x = distance_x / timeline_offset;  // 每秒的gps偏移
                    double delta_y = distance_y / timeline_offset;
                    for ( int j = 0; j < timeline_offset; j++){
                        double[] insert_gps = new double[2];
                        insert_gps[0] = path.get(i)[0] + delta_x * (j+1); // 要插入的坐标
                        insert_gps[1] = path.get(i)[1] + delta_y * (j+1);
                        insert_path.add(insert_gps);
                        insert_timeline.add(timeline.get(i) + j + 1);
                    }
                    new_path.addAll(new_path.size()-1,insert_path);
                    new_timeline.addAll(new_timeline.size()-1,insert_timeline);
                }
                new_path.add(path.get(i));
                new_timeline.add(timeline.get(i));
            }
//            b.setPath(new_path);
//            b.setTimeline(new_timeline);
            batteries.get(k).setPath(new_path);
            batteries.get(k).setTimeline(new_timeline);
        }

        batteriesList.setBattery_gps_list(batteries);
        System.out.printf("finish!, start_time: %d, end_time: %d \n", start_time, end_time);
        return batteriesList;
    }

    /* 获取电池的详细信息（包括单体电压） */
    public BatteryDetails getBatteryDetails(int device_id, int time){
        List<String> time_list = Arrays.asList("0000","0015","0030","0045","0100","0115","0130","0145","0200","0215","0230","0245","0300","0315","0330","0345","0400","0415","0430","0445","0500","0515","0530","0545","0600","0615","0630","0645","0700","0715","0730","0745","0800","0815","0830","0845","0900","0915","0930","0945","1000","1015","1030","1045","1100","1115","1130","1145","1200","1215","1230","1245","1300","1315","1330","1345","1400","1415","1430","1445","1500","1515","1530","1545","1600","1615","1630","1645","1700","1715","1730","1745","1800","1815","1830","1845","1900","1915","1930","1945","2000","2015","2030","2045","2100","2115","2130","2145","2200","2215","2230","2245","2300","2315","2330","2345");
        String now_time = DateUtil.timeStamp_Date(String.valueOf(time), "yyyyMMddHHmm");
//        System.out.println(now_time);
        String table_time = "";
        String temp = now_time.substring(8);
        int tt = Integer.parseInt(temp);
//        System.out.println(tt);
        for ( int i = 0; i < time_list.size(); i++){
            if (tt >= Integer.parseInt(time_list.get(i))){
                table_time = time_list.get(i);
                System.out.println(table_time);
            }
        }
        BatteryDetail battery_details = batteriesMapper.getBatteryDetail(device_id, "bms_" + now_time.substring(0,8) + table_time);
        SingleVoltage battery_vols = batteriesMapper.getBatteryVol(device_id, "singlevoltage_" + now_time.substring(0,8) + table_time);

        BatteryDetails batteryDetails = new BatteryDetails();
        batteryDetails.setBattery_detail_list(battery_details);
        batteryDetails.setBattery_voltages(battery_vols);
        return batteryDetails;
    }

    /* 获取某个时间内所有电池的状态统计 */
    public BatteryState getBatteryState(int time){
        List<String> time_list = Arrays.asList("0000","0015","0030","0045","0100","0115","0130","0145","0200","0215","0230","0245","0300","0315","0330","0345","0400","0415","0430","0445","0500","0515","0530","0545","0600","0615","0630","0645","0700","0715","0730","0745","0800","0815","0830","0845","0900","0915","0930","0945","1000","1015","1030","1045","1100","1115","1130","1145","1200","1215","1230","1245","1300","1315","1330","1345","1400","1415","1430","1445","1500","1515","1530","1545","1600","1615","1630","1645","1700","1715","1730","1745","1800","1815","1830","1845","1900","1915","1930","1945","2000","2015","2030","2045","2100","2115","2130","2145","2200","2215","2230","2245","2300","2315","2330","2345");
        String now_time = DateUtil.timeStamp_Date(String.valueOf(time), "yyyyMMddHHmm");
        System.out.println(now_time);
        String table_time = "";
        String temp = now_time.substring(8);
        int tt = Integer.parseInt(temp);
        for ( int i = 0; i < time_list.size(); i++){
            if (tt >= Integer.parseInt(time_list.get(i))){
                table_time = time_list.get(i);
                System.out.println(table_time);
            }
        }
        System.out.println("--------"+"bms"+now_time.substring(0,8)+table_time);
        List<BatteryDetail> batteryDetail = batteriesMapper.getBatteriesDetail("bms_" + now_time.substring(0,8) + table_time);
        BatteryState batteryState = new BatteryState();
        int rent_num = 0;        // 已租
        int wait_num = 0;        // 待租
        int free_num = 0;        // 空闲
        int charge_num = 0;      // 充电
        int discharge_num = 0;   // 放电
        for (BatteryDetail b : batteryDetail){
            if (b.getElectric() > 30000){discharge_num += 1;}
            else if (b.getElectric() == 0){free_num += 1;}
            else {charge_num += 1;}

            if (b.getRun_mode_ext() == 101){ wait_num += 1;}
            else if (b.getRun_mode_ext() == 102) {rent_num += 1;}
        }
        batteryState.setRent_num(rent_num);
        batteryState.setWait_num(wait_num);
        batteryState.setFree_num(free_num);
        batteryState.setCharge_num(charge_num);
        batteryState.setDischarge_num(discharge_num);

        return batteryState;
    }

    /* 获取某段时间内所有区域的电池数量 */
    public BatteryAreaNum getAreaRank(int day, int time) throws IOException {
        List<Gps> gpsList = batteriesMapper.getBatteryGps("bms_" + day + "0000", "xl_device_gps_" + day, time);
//        List<Gps> gpsList = batteriesMapper.getBatteryGps("bms_202202190000", "xl_device_gps_20220219", 1645200000,1645200000);

        BatteryAreaNum batteryAreaNum = new BatteryAreaNum();
        List<String> province_name = new ArrayList<>();
        List<String> city_name = new ArrayList<>();
        List<Integer> city_num = new ArrayList<>();

        for (Gps gps: gpsList){
            String city = "";
            if (gps.getGps().isEmpty()) continue;
            String[] str_gps = gps.getGps().split(",");
            // 排除无效坐标
            if (str_gps[0].equals("A")){
                double[] lat_lng = GPSTransToAMap.transform(gps.getGps());
//                String s = String.valueOf(lat_lng[0]) + "," + String.valueOf(lat_lng[1]);
//                city = LocationUtil.getCounterLocation(s);
                BatteryArea batteryArea = new BatteryArea();
                batteryArea = GetCityRange.GetLocation(lat_lng);
                //System.out.printf("gps: %s, city: %s\n", s, city);
                if (city_name.size() == 0){
                    province_name.add(batteryArea.province);
                    city_name.add(batteryArea.city);
                    city_num.add(1);
                    System.out.println("11: "+batteryArea.province+"  "+batteryArea.city+"  "+1);
                }
                else{
                    boolean flag = false;
                    for (int i=0; i<city_name.size();i++){
                        if (batteryArea.city.equals(city_name.get(i))){
                            flag = true;
                            int t = city_num.get(i);
                            t += 1;
                            city_num.set(i,t);
                            System.out.println("22: "+province_name.get(i)+"  "+city_name.get(i)+"  "+city_num.get(i));
                            break;
                        }
                    }
                    if (!flag){
                        province_name.add(batteryArea.province);
                        city_name.add(batteryArea.city);
                        city_num.add(1);
                        System.out.println("33: "+batteryArea.province+"  "+batteryArea.city+"  "+1);
                    }
                }
            }
        }
        batteryAreaNum.setNum(city_num);
        batteryAreaNum.setCities(city_name);
        batteryAreaNum.setProvince(province_name);
        System.out.println(batteryAreaNum);
        return batteryAreaNum;
    }

    //仅根据GPS获得区域电动车所有数量
    public BatteryAreaNum getAreaNum(int day) throws IOException {
        List<Gps> gpsList = batteriesMapper.getbGps("xl_device_gps_" + day);
//        List<Gps> gpsList = batteriesMapper.getBatteryGps("bms_202202190000", "xl_device_gps_20220219", 1645200000,1645200000);

        BatteryAreaNum batteryAreaNum = new BatteryAreaNum();
        List<String> province_name = new ArrayList<>();
        List<String> city_name = new ArrayList<>();
        List<Integer> city_num = new ArrayList<>();

        for (Gps gps: gpsList){
            String city = "";
            if (gps.getGps().isEmpty()) continue;
            String[] str_gps = gps.getGps().split(",");
            // 排除无效坐标
            if (str_gps[0].equals("A")){
                double[] lat_lng = GPSTransToAMap.transform(gps.getGps());
//                String s = String.valueOf(lat_lng[0]) + "," + String.valueOf(lat_lng[1]);
//                city = LocationUtil.getCounterLocation(s);
                BatteryArea batteryArea = new BatteryArea();
                batteryArea = GetCityRange.GetLocation(lat_lng);
                //System.out.printf("gps: %s, city: %s\n", s, city);
                if (city_name.size() == 0){
                    province_name.add(batteryArea.province);
                    city_name.add(batteryArea.city);
                    city_num.add(1);
                    System.out.println("11: "+batteryArea.province+"  "+batteryArea.city+"  "+1);
                }
                else{
                    boolean flag = false;
                    for (int i=0; i<city_name.size();i++){
                        if (batteryArea.city.equals(city_name.get(i))){
                            flag = true;
                            int t = city_num.get(i);
                            t += 1;
                            city_num.set(i,t);
                            System.out.println("22: "+province_name.get(i)+"  "+city_name.get(i)+"  "+city_num.get(i));
                            break;
                        }
                    }
                    if (!flag){
                        province_name.add(batteryArea.province);
                        city_name.add(batteryArea.city);
                        city_num.add(1);
                        System.out.println("33: "+batteryArea.province+"  "+batteryArea.city+"  "+1);
                    }
                }
            }
        }
        batteryAreaNum.setNum(city_num);
        batteryAreaNum.setCities(city_name);
        batteryAreaNum.setProvince(province_name);
        System.out.println(batteryAreaNum);
        return batteryAreaNum;
    }

    /* 获取某个月内所有电池的容量变化 */
    public int getBatteryCapacityMonth(){
        List<BatteryCapacity> b0= batteriesMapper.getBatteryCapacity("bms_202202190000");
        List<BatteryCapacity> b1= batteriesMapper.getBatteryCapacity("bms_202202190015");
        List<BatteryCapacity> b2= batteriesMapper.getBatteryCapacity("bms_202202190030");
        List<BatteryCapacity> b3= batteriesMapper.getBatteryCapacity("bms_202202190045");
        List<BatteryCapacity> b4= batteriesMapper.getBatteryCapacity("bms_202202190100");
        List<BatteryCapacity> b5= batteriesMapper.getBatteryCapacity("bms_202202190115");
        List<BatteryCapacity> b6= batteriesMapper.getBatteryCapacity("bms_202202190130");
        List<BatteryCapacity> b7= batteriesMapper.getBatteryCapacity("bms_202202190145");
        List<BatteryCapacity> b8= batteriesMapper.getBatteryCapacity("bms_202202190200");
        List<BatteryCapacity> b9= batteriesMapper.getBatteryCapacity("bms_202202190215");
        List<BatteryCapacity> b10= batteriesMapper.getBatteryCapacity("bms_202202190230");
        List<BatteryCapacity> b11= batteriesMapper.getBatteryCapacity("bms_202202190245");
        List<BatteryCapacity> b12= batteriesMapper.getBatteryCapacity("bms_202202190300");
        List<BatteryCapacity> b13= batteriesMapper.getBatteryCapacity("bms_202202190315");
        List<BatteryCapacity> b14= batteriesMapper.getBatteryCapacity("bms_202202190330");
        List<BatteryCapacity> b15= batteriesMapper.getBatteryCapacity("bms_202202190345");
        List<BatteryCapacity> b16= batteriesMapper.getBatteryCapacity("bms_202202190400");
        List<BatteryCapacity> b17= batteriesMapper.getBatteryCapacity("bms_202202190415");
        List<BatteryCapacity> b18= batteriesMapper.getBatteryCapacity("bms_202202190430");
        List<BatteryCapacity> b19= batteriesMapper.getBatteryCapacity("bms_202202190445");
        List<BatteryCapacity> b20= batteriesMapper.getBatteryCapacity("bms_202202190500");
        List<BatteryCapacity> b21= batteriesMapper.getBatteryCapacity("bms_202202190515");
        List<BatteryCapacity> b22= batteriesMapper.getBatteryCapacity("bms_202202190530");
        List<BatteryCapacity> b23= batteriesMapper.getBatteryCapacity("bms_202202190545");
        List<BatteryCapacity> b24= batteriesMapper.getBatteryCapacity("bms_202202190600");
        List<BatteryCapacity> b25= batteriesMapper.getBatteryCapacity("bms_202202190615");
        List<BatteryCapacity> b26= batteriesMapper.getBatteryCapacity("bms_202202190630");
        List<BatteryCapacity> b27= batteriesMapper.getBatteryCapacity("bms_202202190645");
        List<BatteryCapacity> b28= batteriesMapper.getBatteryCapacity("bms_202202190700");
        List<BatteryCapacity> b29= batteriesMapper.getBatteryCapacity("bms_202202190715");
        List<BatteryCapacity> b30= batteriesMapper.getBatteryCapacity("bms_202202190730");
        List<BatteryCapacity> b31= batteriesMapper.getBatteryCapacity("bms_202202190745");
        List<BatteryCapacity> b32= batteriesMapper.getBatteryCapacity("bms_202202190800");
        List<BatteryCapacity> b33= batteriesMapper.getBatteryCapacity("bms_202202190815");
        List<BatteryCapacity> b34= batteriesMapper.getBatteryCapacity("bms_202202190830");
        List<BatteryCapacity> b35= batteriesMapper.getBatteryCapacity("bms_202202190845");
        List<BatteryCapacity> b36= batteriesMapper.getBatteryCapacity("bms_202202190900");
        List<BatteryCapacity> b37= batteriesMapper.getBatteryCapacity("bms_202202190915");
        List<BatteryCapacity> b38= batteriesMapper.getBatteryCapacity("bms_202202190930");
        List<BatteryCapacity> b39= batteriesMapper.getBatteryCapacity("bms_202202190945");
        List<BatteryCapacity> b40= batteriesMapper.getBatteryCapacity("bms_202202191000");
        List<BatteryCapacity> b41= batteriesMapper.getBatteryCapacity("bms_202202191015");
        List<BatteryCapacity> b42= batteriesMapper.getBatteryCapacity("bms_202202191030");
        List<BatteryCapacity> b43= batteriesMapper.getBatteryCapacity("bms_202202191045");
        List<BatteryCapacity> b44= batteriesMapper.getBatteryCapacity("bms_202202191100");
        List<BatteryCapacity> b45= batteriesMapper.getBatteryCapacity("bms_202202191115");
        List<BatteryCapacity> b46= batteriesMapper.getBatteryCapacity("bms_202202191130");
        List<BatteryCapacity> b47= batteriesMapper.getBatteryCapacity("bms_202202191145");
        List<BatteryCapacity> b48= batteriesMapper.getBatteryCapacity("bms_202202191200");
        List<BatteryCapacity> b49= batteriesMapper.getBatteryCapacity("bms_202202191215");
        List<BatteryCapacity> b50= batteriesMapper.getBatteryCapacity("bms_202202191230");
        List<BatteryCapacity> b51= batteriesMapper.getBatteryCapacity("bms_202202191245");
        List<BatteryCapacity> b52= batteriesMapper.getBatteryCapacity("bms_202202191300");
        List<BatteryCapacity> b53= batteriesMapper.getBatteryCapacity("bms_202202191315");
        List<BatteryCapacity> b54= batteriesMapper.getBatteryCapacity("bms_202202191330");
        List<BatteryCapacity> b55= batteriesMapper.getBatteryCapacity("bms_202202191345");
        List<BatteryCapacity> b56= batteriesMapper.getBatteryCapacity("bms_202202191400");
        List<BatteryCapacity> b57= batteriesMapper.getBatteryCapacity("bms_202202191415");
        List<BatteryCapacity> b58= batteriesMapper.getBatteryCapacity("bms_202202191430");
        List<BatteryCapacity> b59= batteriesMapper.getBatteryCapacity("bms_202202191445");
        List<BatteryCapacity> b60= batteriesMapper.getBatteryCapacity("bms_202202191500");
        List<BatteryCapacity> b61= batteriesMapper.getBatteryCapacity("bms_202202191515");
        List<BatteryCapacity> b62= batteriesMapper.getBatteryCapacity("bms_202202191530");
        List<BatteryCapacity> b63= batteriesMapper.getBatteryCapacity("bms_202202191545");
        List<BatteryCapacity> b64= batteriesMapper.getBatteryCapacity("bms_202202191600");
        List<BatteryCapacity> b65= batteriesMapper.getBatteryCapacity("bms_202202191615");
        List<BatteryCapacity> b66= batteriesMapper.getBatteryCapacity("bms_202202191630");
        List<BatteryCapacity> b67= batteriesMapper.getBatteryCapacity("bms_202202191645");
        List<BatteryCapacity> b68= batteriesMapper.getBatteryCapacity("bms_202202191700");
        List<BatteryCapacity> b69= batteriesMapper.getBatteryCapacity("bms_202202191715");
        List<BatteryCapacity> b70= batteriesMapper.getBatteryCapacity("bms_202202191730");
        List<BatteryCapacity> b71= batteriesMapper.getBatteryCapacity("bms_202202191745");
        List<BatteryCapacity> b72= batteriesMapper.getBatteryCapacity("bms_202202191800");
        List<BatteryCapacity> b73= batteriesMapper.getBatteryCapacity("bms_202202191815");
        List<BatteryCapacity> b74= batteriesMapper.getBatteryCapacity("bms_202202191830");
        List<BatteryCapacity> b75= batteriesMapper.getBatteryCapacity("bms_202202191845");
        List<BatteryCapacity> b76= batteriesMapper.getBatteryCapacity("bms_202202191900");
        List<BatteryCapacity> b77= batteriesMapper.getBatteryCapacity("bms_202202191915");
        List<BatteryCapacity> b78= batteriesMapper.getBatteryCapacity("bms_202202191930");
        List<BatteryCapacity> b79= batteriesMapper.getBatteryCapacity("bms_202202191945");
        List<BatteryCapacity> b80= batteriesMapper.getBatteryCapacity("bms_202202192000");
        List<BatteryCapacity> b81= batteriesMapper.getBatteryCapacity("bms_202202192015");
        List<BatteryCapacity> b82= batteriesMapper.getBatteryCapacity("bms_202202192030");
        List<BatteryCapacity> b83= batteriesMapper.getBatteryCapacity("bms_202202192045");
        List<BatteryCapacity> b84= batteriesMapper.getBatteryCapacity("bms_202202192100");
        List<BatteryCapacity> b85= batteriesMapper.getBatteryCapacity("bms_202202192115");
        List<BatteryCapacity> b86= batteriesMapper.getBatteryCapacity("bms_202202192130");
        List<BatteryCapacity> b87= batteriesMapper.getBatteryCapacity("bms_202202192145");
        List<BatteryCapacity> b88= batteriesMapper.getBatteryCapacity("bms_202202192200");
        List<BatteryCapacity> b89= batteriesMapper.getBatteryCapacity("bms_202202192215");
        List<BatteryCapacity> b90= batteriesMapper.getBatteryCapacity("bms_202202192230");
        List<BatteryCapacity> b91= batteriesMapper.getBatteryCapacity("bms_202202192245");
        List<BatteryCapacity> b92= batteriesMapper.getBatteryCapacity("bms_202202192300");
        List<BatteryCapacity> b93= batteriesMapper.getBatteryCapacity("bms_202202192315");
        List<BatteryCapacity> b94= batteriesMapper.getBatteryCapacity("bms_202202192330");
        List<BatteryCapacity> b95= batteriesMapper.getBatteryCapacity("bms_202202192345");
        System.out.printf("finish\n");

        BatteryPowerUsage batteryPowerUsage = new BatteryPowerUsage();
        int usage = 0;
        for (int i = 0; i < b0.size(); i++){
            System.out.printf("i: ", b0.get(i).getDevice_id());
            int id = b0.get(i).getDevice_id();
            int pre = b0.get(i).getLastcapacity();
            int now = b1.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b2.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b3.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b4.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b5.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b6.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b7.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b8.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b9.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b10.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b11.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b12.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b13.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b14.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b15.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b16.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b17.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b18.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b19.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b20.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b21.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b22.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b23.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b24.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b25.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b26.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b27.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b28.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b29.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b30.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b31.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b32.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b33.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b34.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b35.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b36.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b37.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b38.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b39.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b40.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b41.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b42.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b43.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b44.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b45.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b46.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b47.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b48.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b49.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b50.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b51.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b52.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b53.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b54.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b55.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b56.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b57.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b58.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b59.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b60.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b61.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b62.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b63.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b64.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b65.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b66.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b67.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b68.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b69.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b70.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b71.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b72.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b73.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b74.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b75.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b76.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b77.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b78.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b79.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b80.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b81.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b82.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b83.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b84.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b85.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b86.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b87.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b88.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b89.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b90.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b91.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b92.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b93.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
            now = b94.get(i).getLastcapacity();
            if (pre > now) usage += (pre-now);
            else pre = now;
        }
        // 561920299 mAh
        // 3371521.794 kwh
        // 按 59kgCO₂/kWh 计算
        // 1989197.39 kg碳排放
        return usage;
    }
}
